package io.renren.modules.live.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.modules.live.entity.FanweAnchorEntity;
import io.renren.modules.live.entity.FanweUserEntity;
import io.renren.modules.live.service.FanweAnchorService;
import io.renren.modules.live.service.FanweUserService;
import io.renren.modules.live.vo.saveBankCardVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.live.entity.FanweBankCardEntity;
import io.renren.modules.live.service.FanweBankCardService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author shaoqi
 * @email sunlightcs@gmail.com
 * @date 2020-11-20 16:07:22
 */
@Log4j2
@Api(tags = "银行卡管理")
@RestController
@RequestMapping("live/fanwebankcard")
public class FanweBankCardController {
    @Autowired
    private FanweBankCardService fanweBankCardService;
    @Autowired
    private FanweUserService fanweUserService;

    @Autowired
    private FanweAnchorService fanweAnchorService;


    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("live:fanwebankcard:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fanweBankCardService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @RequiresPermissions("live:fanwebankcard:info")
    public R info( Integer bankCardId){
		FanweBankCardEntity fanweBankCard = fanweBankCardService.getById(bankCardId);

        return R.ok().put("fanweBankCard", fanweBankCard);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("live:fanwebankcard:save")
    public R save(@RequestBody FanweBankCardEntity fanweBankCard){
		fanweBankCardService.save(fanweBankCard);
        return R.ok();
    }

//    public static void main(String[] args) {
//        String s = DigestUtils.sha256Hex("0000");
//        System.out.println(s);
//    }
    @ApiOperation("用户或主播根据ID绑定添加银行卡信息")
    @PostMapping("/saveBankCard")
    //@RequiresPermissions("live:fanwebankcard:save")
    public R saveBankCard(@RequestBody saveBankCardVo bankCardVo, HttpServletRequest request){
        String token = request.getHeader("token");
        String s = token.substring(0, 3);

            if(s.equals("usr")){
                String payPwd = DigestUtils.sha256Hex(bankCardVo.getPaypassword());
                FanweUserEntity userEntity = fanweUserService.getById(bankCardVo.getUserId());
                if(userEntity.getPaypassword().equals(payPwd)){
                    //根据id查询有多少张卡  超过5张不能添加  相同卡号不添加
                    List<FanweBankCardEntity>  bankCardList= fanweBankCardService.getByUserId(bankCardVo.getUserId());
                    if(bankCardList.isEmpty()&&bankCardList.size()==0){
                        FanweBankCardEntity bankCardEntity = new FanweBankCardEntity();
                        bankCardEntity.setCreateTime(new Date(System.currentTimeMillis()));
                        bankCardEntity.setAccountOpeningNetwork(bankCardVo.getAccountOpeningNetwork());
                        bankCardEntity.setBankName(bankCardVo.getBankName());
                        bankCardEntity.setBankNumber(bankCardVo.getBankNumber());
                        bankCardEntity.setIsDefault(bankCardVo.getIsDefault());
                        bankCardEntity.setUserId(bankCardVo.getUserId());
                        bankCardEntity.setBankOfDeposit(bankCardVo.getBankOfDeposit());
                        bankCardEntity.setProvincesCityArea(bankCardVo.getProvincesCityArea());
                        fanweBankCardService.save(bankCardEntity);
                    }else {
                        if(bankCardList.size()<5){
                            for(FanweBankCardEntity list:bankCardList){
                                if(list.getBankNumber().equals(bankCardVo.getBankNumber())){
                                    return R.error("当前银行卡已添加过了");
                                }
                            }
                            FanweBankCardEntity bankCardEntity = new FanweBankCardEntity();
                            bankCardEntity.setCreateTime(new Date(System.currentTimeMillis()));
                            bankCardEntity.setAccountOpeningNetwork(bankCardVo.getAccountOpeningNetwork());
                            bankCardEntity.setBankName(bankCardVo.getBankName());
                            bankCardEntity.setBankNumber(bankCardVo.getBankNumber());
                            bankCardEntity.setIsDefault(bankCardVo.getIsDefault());
                            bankCardEntity.setUserId(bankCardVo.getUserId());
                            bankCardEntity.setBankOfDeposit(bankCardVo.getBankOfDeposit());
                            bankCardEntity.setProvincesCityArea(bankCardVo.getProvincesCityArea());
                            fanweBankCardService.save(bankCardEntity);
                        }else {
                            return R.error("您的银行卡添加过多了");
                        }
                    }
                }else {
                    return R.error("支付密码不对,请重新输入支付密码");
                }
            }else if(s.equals("anc")){
                String payPwd = DigestUtils.sha256Hex(bankCardVo.getPaypassword());
                FanweAnchorEntity anchorEntity = fanweAnchorService.getById(bankCardVo.getUserId());
                if(anchorEntity.getPaypassword().equals(payPwd)){
                    //根据id查询有多少张卡  超过5张不能添加  相同卡号不添加
                    List<FanweBankCardEntity>  bankCardList= fanweBankCardService.getByUserId(bankCardVo.getUserId());
                    if(bankCardList.isEmpty()&&bankCardList.size()==0){
                        FanweBankCardEntity bankCardEntity = new FanweBankCardEntity();
                        bankCardEntity.setCreateTime(new Date(System.currentTimeMillis()));
                        bankCardEntity.setAccountOpeningNetwork(bankCardVo.getAccountOpeningNetwork());
                        bankCardEntity.setBankName(bankCardVo.getBankName());
                        bankCardEntity.setBankNumber(bankCardVo.getBankNumber());
                        bankCardEntity.setIsDefault(bankCardVo.getIsDefault());
                        bankCardEntity.setUserId(bankCardVo.getUserId());
                        bankCardEntity.setBankOfDeposit(bankCardVo.getBankOfDeposit());
                        bankCardEntity.setProvincesCityArea(bankCardVo.getProvincesCityArea());
                        fanweBankCardService.save(bankCardEntity);
                    }else {
                        if(bankCardList.size()<5){
                            for(FanweBankCardEntity list:bankCardList){
                                if(list.getBankNumber().equals(bankCardVo.getBankNumber())){
                                    return R.error("当前银行卡已添加过了");
                                }
                            }
                            FanweBankCardEntity bankCardEntity = new FanweBankCardEntity();
                            bankCardEntity.setCreateTime(new Date(System.currentTimeMillis()));
                            bankCardEntity.setAccountOpeningNetwork(bankCardVo.getAccountOpeningNetwork());
                            bankCardEntity.setBankName(bankCardVo.getBankName());
                            bankCardEntity.setBankNumber(bankCardVo.getBankNumber());
                            bankCardEntity.setIsDefault(bankCardVo.getIsDefault());
                            bankCardEntity.setUserId(bankCardVo.getUserId());
                            bankCardEntity.setBankOfDeposit(bankCardVo.getBankOfDeposit());
                            bankCardEntity.setProvincesCityArea(bankCardVo.getProvincesCityArea());
                            fanweBankCardService.save(bankCardEntity);
                        }else {
                            return R.error("您的银行卡添加过多了");
                        }
                    }
                }else {
                    return R.error("支付密码不对,请重新输入支付密码");
                }
            }
        return R.ok("添加成功");
    }


    @ApiOperation("根据用户id查询所有银行卡信息")
    @GetMapping("/findByIdBankCardList")
    //@RequiresPermissions("live:fanwebankcard:update")
    public R findByIdBankCardList(@ApiParam(value = "用户id",required = true) Long userId){
        List<FanweBankCardEntity> bankCardList = fanweBankCardService.getByUserId(userId);
        return R.ok("查询成功").put("bankCardList",bankCardList);
    }


    @ApiOperation("根据用户id删除银行卡信息")
    @DeleteMapping("/deleteByUserId")
    //@RequiresPermissions("live:fanwebankcard:delete")
    public R deleteByUserId(@ApiParam(value = "用户id",required = true) Long userId,
                            @ApiParam(value = "银行卡号",required = true) String bankNumber){
        /**
         * 或者先查 判断 后删除
         */
        try {
            fanweBankCardService.deleteByUserId(userId,bankNumber);
        }catch (Exception e){
            log.info(e);
        }
        return R.ok("删除成功");
    }


    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("live:fanwebankcard:update")
    public R update(@RequestBody FanweBankCardEntity fanweBankCard){
		fanweBankCardService.updateById(fanweBankCard);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("live:fanwebankcard:delete")
    public R delete(@RequestBody Integer[] bankCardIds){
		fanweBankCardService.removeByIds(Arrays.asList(bankCardIds));
        return R.ok();
    }

}
